﻿Imports System.Data.OleDb
Public Class AñadirTopicosYRespuestas

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim sinonimo As String
        sinonimo = InputBox("Nombre para el sinónimo: ", "NOMBRE SINÓNIMO")
        If sinonimo = "" Then Exit Sub
        If rtxt_TOPICO.Text = "" Then
            MsgBox("No has escrito ningún sinónimo", , "NO HAY SINÓNIMO")
            rtxt_TOPICO.Focus()
            Exit Sub
        End If
        lsb_SINONIMO_DEL_TOPICO.Items.Add(sinonimo)

        '==============================================================================================
        'COMENTARIOS. Procedimiento encargado de GRABAR EL SINONIMO DEL TOPICO EN LA B.D.
        '==============================================================================================

        'Se establece la conexión con la base de datos.
        Dim sCnn As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & txtBase
        Dim ODbConn As New OleDb.OleDbConnection(sCnn)
        ODbConn.Open()

        Dim Command As New OleDb.OleDbCommand("INSERT INTO PSIS_SINONIMOS_TOPICOS (psi,topico,sinonimo) VALUES (" & "'" & Main.txt_Nombre_PSI.Text & "'," & "'" & rtxt_TOPICO.Text & "'," & "'" & sinonimo & "')", ODbConn)
        Dim DataReader As OleDbDataReader = Command.ExecuteReader(CommandBehavior.CloseConnection)

        Command.Dispose()
        ODbConn.Close()

        Me.Button3.Focus()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        '==============================================================================================
        'COMENTARIOS. ELIMINAR UN SINÓNIMO DE UN TÓPICO DEL PSI DE LA B.D.
        '==============================================================================================

        Dim sCnn As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & txtBase
        Dim ODbConn As New OleDb.OleDbConnection(sCnn)
        ODbConn.Open()

        Dim Command As New OleDb.OleDbCommand("DELETE FROM PSIS_SINONIMOS_TOPICOS WHERE PSIS_SINONIMOS_TOPICOS.topico = " & "'" & rtxt_TOPICO.Text & "'" & " and " & "PSIS_SINONIMOS_TOPICOS.sinonimo= " & "'" & lsb_SINONIMO_DEL_TOPICO.SelectedItem & "'" & " and " & "PSIS_SINONIMOS_TOPICOS.psi= " & "'" & lbl_NOMBRE_PSI_AÑADIR_TOPICOS_Y_RESPUESTAS.Text & "'", ODbConn)
        Dim DataReader As OleDb.OleDbDataReader = Command.ExecuteReader(CommandBehavior.CloseConnection)

        'Libera memoria
        Command.Dispose()
        ODbConn.Close()

        lsb_SINONIMO_DEL_TOPICO.Items.Remove(lsb_SINONIMO_DEL_TOPICO.SelectedItem)
        lsb_SINONIMO_DEL_TOPICO.Focus()
        lsb_SINONIMO_DEL_TOPICO.SelectedItem = 0
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Select Case Main.TipoLlamadaAñadirTopicosYRespuestas

            Case "AÑADIR"

                'Se establece la conexión con la base de datos.
                Dim sCnn As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & txtBase
                Dim ODbConn As New OleDb.OleDbConnection(sCnn)
                ODbConn.Open()

                If rtxt_TOPICO.Text <> "" And rtxt_PSI_AÑADIRTOPICOS_Y_RESPUESTAS_RESPUESTA.Text <> "" Then
                    Dim Command As New OleDb.OleDbCommand("SELECT * FROM PSIS_SINONIMOS_TOPICOS", ODbConn)
                    Dim DataReader As OleDbDataReader = Command.ExecuteReader(CommandBehavior.CloseConnection)
                    Dim ValorDelTopico As String
                    While DataReader.Read
                        ValorDelTopico = DataReader.Item("topico")
                        If UCase(rtxt_TOPICO.Text) = UCase(ValorDelTopico) Then
                            MsgBox("El tópico ya existe en el listado, aún así lo añado...", , "TÓPICO DUPLICADO")
                        End If
                    End While

                    Main.lsb_PSIS_TOPICO.Items.Add(rtxt_TOPICO.Text)
                    Main.lsb_PSIS_RESPUESTA.Items.Add(rtxt_PSI_AÑADIRTOPICOS_Y_RESPUESTAS_RESPUESTA.Text)

                    Dim AñadirTopicoCommand As New OleDb.OleDbCommand("INSERT INTO PSIS_TOPICOS_TABULADOS (PSI,topico,respuesta) VALUES (" & "'" & Main.txt_Nombre_PSI.Text & "', " & "'" & rtxt_TOPICO.Text & "', " & "'" & rtxt_PSI_AÑADIRTOPICOS_Y_RESPUESTAS_RESPUESTA.Text & "')", ODbConn)
                    Dim AñadirTopicoDataReader As OleDbDataReader = AñadirTopicoCommand.ExecuteReader(CommandBehavior.CloseConnection)

                    'Liberar Memoria
                    Command.Dispose()
                    AñadirTopicoCommand.Dispose()
                    ODbConn.Close()
                    Me.Close()

                Else
                    MsgBox("Tienes que escribir un tópico y una respueta para ese tópico")
                End If


            Case "MODIFICAR"
                Dim sCnn As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & txtBase
                Dim ODbConn As New OleDb.OleDbConnection(sCnn)
                ODbConn.Open()

                Dim ModificarTopicoCommand As New OleDb.OleDbCommand("UPDATE PSIS_TOPICOS_TABULADOS SET PSIS_TOPICOS_TABULADOS.psi= " & "'" & Main.txt_Nombre_PSI.Text & "', topico= " & "'" & rtxt_TOPICO.Text & "', respuesta=" & "'" & rtxt_PSI_AÑADIRTOPICOS_Y_RESPUESTAS_RESPUESTA.Text & "' WHERE ID = " & CType(Main.lbl_NRO_DE_TOPICO_EN_BD.Text, Integer), ODbConn)
                Dim ModificarTopicoDataReader As OleDbDataReader = ModificarTopicoCommand.ExecuteReader(CommandBehavior.CloseConnection)

                Dim TopicCommand As New OleDbCommand("SELECT * FROM PSIS_TOPICOS_TABULADOS WHERE PSI = " & "'" & Main.txt_Nombre_PSI.Text & "'", ODbConn)
                Dim TopicReader As OleDbDataReader = TopicCommand.ExecuteReader(CommandBehavior.CloseConnection)

                Main.lsb_PSIS_TOPICO.Items.Clear()
                Main.lsb_PSIS_RESPUESTA.Items.Clear()
                While TopicReader.Read
                    Main.lsb_PSIS_TOPICO.Items.Add(TopicReader.Item("Topico"))
                    Main.lsb_PSIS_RESPUESTA.Items.Add(TopicReader.Item("Respuesta"))
                End While

                TopicCommand.Dispose()
                ModificarTopicoCommand.Dispose()
                ODbConn.Close()
                Me.Close()


        End Select




    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub rtxt_TOPICO_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles rtxt_TOPICO.KeyPress
        '    Dim x As Char = e.KeyChar
        '   e.KeyChar = ChrW(System.Windows.Forms.Keys.Enter)

   
        If e.KeyChar = ChrW(System.Windows.Forms.Keys.Enter) Then
            rtxt_PSI_AÑADIRTOPICOS_Y_RESPUESTAS_RESPUESTA.Focus()
        End If
    End Sub

    Private Sub rtxt_TOPICO_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles rtxt_TOPICO.LostFocus

        If rtxt_TOPICO.Text.Contains(",") = True Then
            MsgBox("No está permitido introducir el carácter ',' en un tópico.")
            rtxt_TOPICO.Focus()
            Exit Sub
        End If
        If rtxt_TOPICO.Text.Contains(".") = True Then
            MsgBox("No está permitido introducir el carácter '.' en un tópico.")
            rtxt_TOPICO.Focus()
            Exit Sub
        End If
        If rtxt_TOPICO.Text.Contains("!") = True Then
            MsgBox("No está permitido introducir el carácter '!' en un tópico.")
            rtxt_TOPICO.Focus()
            Exit Sub
        End If
        If rtxt_TOPICO.Text.Contains("?") = True Then
            MsgBox("No está permitido introducir el carácter '?' en un tópico.")
            rtxt_TOPICO.Focus()
            Exit Sub
        End If

    End Sub

    Private Sub rtxt_TOPICO_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtxt_TOPICO.TextChanged

    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        DarFormatoTexto()
    End Sub

    Private Sub AñadirTopicosYRespuestas_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        rtxt_TOPICO.Focus()
    End Sub

    Private Sub AñadirTopicosYRespuestas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub rtxt_PSI_AÑADIRTOPICOS_Y_RESPUESTAS_RESPUESTA_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles rtxt_PSI_AÑADIRTOPICOS_Y_RESPUESTAS_RESPUESTA.LostFocus
        'DE MOMENTO NO ES OPERATIVO
        '   rtxt_PSI_AÑADIRTOPICOS_Y_RESPUESTAS_RESPUESTA.Text = CombrobarPuntoFinal(rtxt_PSI_AÑADIRTOPICOS_Y_RESPUESTAS_RESPUESTA.Text)
    End Sub

End Class